//
//  UpStairSliderView.m
//  CarFace
//
//  Created by dlios on 15/3/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UpStairSliderView.h"
#import "UpStairCell.h"
@interface UpStairSliderView () < UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>


@property(nonatomic, retain)NSIndexPath *indexPath;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UIView *view;


@property(nonatomic, retain)NSIndexPath *path;
//@property(nonatomic, retain)UICollectionView *collectionBottom;
@end
@implementation UpStairSliderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [_path release];
    [_imageRight release];
    [_indexPath release];
    [_blackLook release];
    [_arr release];
    [_collectionView release];
    [_button release];
    [_view release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       //右侧图片栏
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button.frame = CGRectMake(frame.size.width - frame.size.height, 0, frame.size.height, frame.size.height);

//        UICollectionView;
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        //设置单个大小
        flowlayout.itemSize = CGSizeMake(frame.size.width / 10, frame.size.height);
        flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowlayout.minimumInteritemSpacing = 0;
        flowlayout.minimumLineSpacing = 0;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width - frame.size.height, frame.size.height) collectionViewLayout:flowlayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.collectionView];
        [flowlayout release];
        [_collectionView release];
        self.collectionView.backgroundColor = [UIColor clearColor];

        [self.collectionView registerClass:[UpStairCell class] forCellWithReuseIdentifier:@"LittleBitch"];
        [self addSubview:self.button];
 
    }
    return self;
}


// 内部改变
- (void)setBlackLook:(NSIndexPath *)blackLook
{
    _blackLook = blackLook;
//    NSLog(@"-----%ld", (long)blackLook.row);

//    [self.collectionView setContentOffset:CGPointMake(_collectionView.contentSize.width / ( self.arr.count * 1.5) * blackLook.row, 0) animated:YES];
//    self.indexPath = blackLook;
//#warning what a fuck bitch;
//    [_collectionView selectItemAtIndexPath:blackLook animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
    
    //偏移量改变, 颜色字体改变;
    if (_rec == 99) {
        
    
    UpStairCell *cell = (UpStairCell *)[_collectionView cellForItemAtIndexPath:self.blackLook];
    cell.changeCS = YES;

    UpStairCell *cellPath = (UpStairCell *)[_collectionView cellForItemAtIndexPath:self.path];
    cellPath.changeCS = NO;
     self.path = blackLook;
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UpStairCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LittleBitch" forIndexPath:indexPath];
    cell.label.text = self.arr[indexPath.item];
//    if (self.indexPath.section == indexPath.section && self.indexPath.row == indexPath.row) {
//        cell.changeCS = YES;
//    } else {
//        cell.changeCS = NO;
//    }
    
    return cell;
    
}

// Collection点击实现的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 点击则改变contentView
//    if (self.indexPath) {
//        UpStairCell *cell = (UpStairCell *)[collectionView cellForItemAtIndexPath:self.indexPath];
//        [self updateCollectionViewCellStatus:cell selected:NO];
//    }
//    UpStairCell *cell = (UpStairCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    self.indexPath = indexPath;
//    [self updateCollectionViewCellStatus:cell selected:YES];
    
    if (_rec == 99) {
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentSize.width / ( self.arr.count * 1.5) * indexPath.item, 0) animated:YES];
    }
    if (_rec == 199) {
        UpStairCell *cellPath = (UpStairCell *)[self.collectionView cellForItemAtIndexPath:self.path];
        cellPath.changeLar = YES;
    }
    
    //执行说好的方法
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeCollection:)]) {
        [self.delegate changeCollection:indexPath.row];
    }

}

// 改变颜色的方法
-(void)updateCollectionViewCellStatus:(UpStairCell *)myCollectionCell selected:(BOOL)selected
{
    myCollectionCell.label.font = selected ?[UIFont systemFontOfSize:16]:[UIFont systemFontOfSize:15];
    myCollectionCell.view.backgroundColor = selected ? [UIColor orangeColor] : [UIColor clearColor];
}

+ (instancetype)upStairWithFrame:(CGRect)frame arr:(NSMutableArray *)arr image:(UIImage *)imageRight action:(SEL)action target:(id)target
{
    UpStairSliderView *upVC = [[UpStairSliderView alloc] initWithFrame:frame];
    upVC.action = action;
    upVC.imageRight = imageRight;
    upVC.arr = arr;
    upVC.target = target;
    [upVC.button addTarget:upVC.target action:upVC.action forControlEvents:UIControlEventTouchUpInside];
     [upVC.button setImage:upVC.imageRight forState:UIControlStateNormal];
    
    return [upVC autorelease];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.arr[indexPath.row] length] == 2 ) {
        return CGSizeMake(44, 44);
    }else if([self.arr[indexPath.row] length] == 3){
        return CGSizeMake(50, 44);
    }else if([self.arr[indexPath.row] length] == 4){
        return CGSizeMake(70, 44);
    }else if([self.arr[indexPath.row] length] == 5){
        return CGSizeMake(90, 44);
    }else if([self.arr[indexPath.row] length] == 6){
        return CGSizeMake(100, 44);
    }
    return CGSizeMake(44, 5);
}


@end
